package com.sitech.openapi.controller;

import com.sitech.openapi.domain.Test;
import com.sitech.openapi.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.Map;
@Api(tags = "工号测试",description = "工号相关请求测试控制层")
@RestController
public class TestController {

    @Autowired
    TestService testService;

    @GetMapping("/query/test/{code}")
    @ApiOperation(value = "测试查询StaffName",notes = "测试使用直接跟参数的形式")
    public ResponseEntity<Test> queryTestByPara(@PathVariable String code,
                                          @RequestHeader HttpHeaders headers){
        Test test = testService.queryTest(code);
        String appeKey = headers.getFirst("app-key");
        HttpHeaders httpHeaders = new HttpHeaders();
        headers.set("app-key",appeKey);
        ResponseEntity<Test> res = new ResponseEntity<Test>(test,headers, HttpStatus.OK);
        return  res;
    }

    @GetMapping("/query/getName")
    @ApiOperation(value = "test query for staff name",notes = "测试使用问号的形式")
    public ResponseEntity<Test> queryTest(@RequestParam String code,
                                          @RequestHeader HttpHeaders headers){
        Test test = testService.queryTest(code);
        String appKey = headers.getFirst("app-key");
        HttpHeaders httpHeaders = new HttpHeaders();
        headers.set("app-key",appKey);
        ResponseEntity<Test> resp = new ResponseEntity<Test>(test,headers, HttpStatus.OK);
        return  resp;
    }

    @ExceptionHandler
    public ResponseEntity<Map<String, String>> ContactTaskError(Exception e, NativeWebRequest request) {


        Map<String, String> errors = new HashMap<>();
        String appKey = request.getHeader("app-key");
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message",e.toString());
        errors.put("referenceError","");

        //HttpHeaders header = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("app-key",appKey);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
